package com.king.mvpdemo.newfragment.presenter;

import com.king.mvpdemo.base.BasePresenter;
import com.king.mvpdemo.net.RxScheduler;
import com.king.mvpdemo.newfragment.bean.ListBean;
import com.king.mvpdemo.newfragment.contract.NewsContract;
import com.king.mvpdemo.newfragment.model.NewsModel;

import io.reactivex.functions.Consumer;

/**
 * @Author King
 * @Date 2019/8/8 14:40
 */
public class NewsPresenter extends BasePresenter<NewsContract.View> implements NewsContract.Presenter {

    NewsContract.Model model;

    public NewsPresenter() {
        model = new NewsModel();
    }

    @Override
    public void getDate(String type, String key) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getDate(type, key).compose(RxScheduler.<ListBean>Flo_io_main())
                .as(mView.<ListBean>bindAutoDispose())
                .subscribe(new Consumer<ListBean>() {
                    @Override
                    public void accept(ListBean listBean) throws Exception {
                        mView.onSuccess(listBean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });

    }

}
